<template>
	<view>

		<view>用户id</view>
		<uni-easyinput v-model="userid"></uni-easyinput>
		<br />

		<button type="primary" size="mini" @click="login">登录</button>

	</view>
</template>

<script>
	export default {
		data() {
			return {
				userid: '',
			};
		},
		methods: {
			login() {

				//断开之前的连接
				uni.closeSocket({

				})

				let url = "";
				//进行websocket连接
				uni.connectSocket({
					url: 'ws://localhost:9501/websocket/message/' + this.userid
				})
				let that = this;
				uni.onSocketOpen(function(res) {
					console.log('WebSocket连接已打开！');
					//这里才能发送消息
					let val = {
						res: res,
						status: true,
					}
					//保存当前用户id，代表登录成功
					uni.setStorageSync("login:userid", that.userid);

					uni.$emit("onSocketOpen", val); //发送全局事件
					//进入主页
					uni.switchTab({
						url: "/pages/msg/msg"
					})
				});
				uni.onSocketError(function(res) {
					console.log('WebSocket连接打开失败，请检查！');
					let val = {
						res: res,
						status: false,
					}
					uni.$emit("onSocketOpen", val); //发送全局事件
				});
			},


		}
	}
</script>

<style lang="scss">

</style>